﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Eassets.Layered.Domain.Global;

namespace Eassets.Data.Global
{
    public class WorkFlowDocumentData
    {
        /// <summary>
        /// Inserts workFlowDocument.
        /// </summary>
        /// <param name="workFlowDocument"></param>
        public void AddWorkFlowDocument(WorkFlowDocument workFlowDocument)
        {
            SqlCommand command = new SqlCommand("SP_AddWorkFlowDocument"); // Name of the SP
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, workFlowDocument));
        }

        /// <summary>
        /// Updates workFlowDocument.
        /// </summary>
        /// <param name="workFlowDocument"></param>
        public void ModifyWorkFlowDocument(WorkFlowDocument workFlowDocument)
        {
            SqlCommand command = new SqlCommand("SP_ModifyWorkFlowDocument"); // Name of the SP
            command.Parameters.AddWithValue("@WorkFlowDocumentId", workFlowDocument.WorkFlowDocumentId);
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, workFlowDocument));
        }


        /// <summary>
        /// Delete workFlowDocument.
        /// </summary>
        /// <param name="workFlowDocumentId"></param>
        public void DeleteWorkFlowDocument(long workFlowDocumentId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteWorkFlowDocument"); // Name of the SP
            command.Parameters.AddWithValue("@WorkFlowDocumentId", workFlowDocumentId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// GetbyId workFlowDocument.
        /// </summary>
        /// <param name="workFlowDocumentId"></param>
        /// <returns></returns>
        public WorkFlowDocument GetByIdWorkFlowDocument(long workFlowDocumentId)
        {
            SqlCommand command = new SqlCommand("SP_GetWorkFlowDocumentById");
            command.Parameters.AddWithValue("@WorkFlowDocumentId", workFlowDocumentId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            WorkFlowDocument workFlowDocument = new WorkFlowDocument();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["WorkFlowDocumentId"].ToString())))
                {
                    workFlowDocument.WorkFlowDocumentId = Convert.ToInt64(dataReader["WorkFlowDocumentId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Code"].ToString())))
                {
                    workFlowDocument.Code = Convert.ToString(dataReader["Code"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    workFlowDocument.Description = Convert.ToString(dataReader["Description"]);
                }
            }
            dataReader.Close();
            return workFlowDocument;
        }

        /// <summary>
        /// GetAll workFlowDocument.
        /// </summary>
        /// <returns></returns>
        public Collection<WorkFlowDocument> GetAllWorkFlowDocument()
        {
            Collection<WorkFlowDocument> workFlowDocuments = new Collection<WorkFlowDocument>();
            SqlCommand command = new SqlCommand("SP_GetAllWorkFlowDocuments");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            WorkFlowDocument workFlowDocument;
            while (dataReader.Read())
            {
                workFlowDocument = new WorkFlowDocument();
                if (!(String.IsNullOrEmpty(dataReader["WorkFlowDocumentId"].ToString())))
                {
                    workFlowDocument.WorkFlowDocumentId = Convert.ToInt64(dataReader["WorkFlowDocumentId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Code"].ToString())))
                {
                    workFlowDocument.Code = Convert.ToString(dataReader["Code"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    workFlowDocument.Description = Convert.ToString(dataReader["Description"]);
                }

                workFlowDocuments.Add(workFlowDocument);
            }
            dataReader.Close();
            return workFlowDocuments;
        }

        public SqlCommand ConstructCommand(SqlCommand command, WorkFlowDocument workFlowDocument)
        {
            command.CommandType = CommandType.StoredProcedure;

            try
            {
                command.Parameters.AddWithValue("@Code", workFlowDocument.Code);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Description", workFlowDocument.Description);
            }
            catch (Exception)
            { }
            return command;
        }
    }
}

